package com.example.springboot.controller;


import com.example.springboot.pojo.Department;
import com.example.springboot.pojo.Result;
import com.example.springboot.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/getAllDepartment")
    public Result<?> getAllDepartment(){
        List<Department> list = departmentService.getAllDepartment();
        return Result.success(list);
    }

    @PostMapping("/addDepartment")
    public Result<?> addDepartment(String department){
        try{
            if(departmentService.findDepartment(department) != null){
                return Result.error("此部门已经存在，不可再次添加");
            }
            departmentService.addDepartment(department);
            return Result.success();
        }catch (Exception e){
            return Result.error("服务异常");
        }
    }

    @PutMapping("/deleteDepartment")
    public Result<?> deleteDepartment(Integer id){
        try{
            departmentService.deleteDepartment(id);
            return Result.success();
        }catch (Exception e){
            return Result.error("服务异常");
        }
    }

    @PostMapping("/updateDepartment")
    public Result<?> updateDepartment(Integer id,String department){
        try{
            departmentService.updateDepartment(id,department);
            return Result.success();
        }catch (Exception e){
            return Result.error("服务异常");
        }
    }
}
